package edu.cs346.group5.ui.userpages;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.Image;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.Caret;

import net.miginfocom.swing.MigLayout;
import javax.swing.JButton;
import java.awt.Panel;
import javax.swing.JTextField;
import java.awt.Canvas;
import java.awt.TextField;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JComboBox;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.net.URL;
import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

import javax.swing.JList;

import edu.cs346.group5.datastores.Topic;
import edu.cs346.group5.datastores.User;
import edu.cs346.group5.logic.Login;
import edu.cs346.group5.logic.Profile;
import edu.cs346.group5.ui.MenuFactory;
import edu.cs346.group5.ui.forumpages.view_topic;
import edu.cs346.group5.ui.userpages.user_page.ListItem;
import edu.cs346.group5.logic.ViewTopic;

import javax.swing.JTable;
import javax.swing.JScrollBar;


public class user_page extends JFrame {

	private JPanel contentPane;
	private JLabel lblFirstname;
	private JLabel lblLastname;
	private JLabel lblEmail;
	private JMenu mnSearchbrowse;
	private JTextField txtProfilePictureUrl;
	

	/**
	 * Launch the application.
	 */
	

	/**
	 * Create the frame.
	 */
	public user_page(final User user, final boolean isUser) {
		setTitle("User Profile");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 800, 600);

		setJMenuBar(MenuFactory.getMenu(this));
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		
		lblFirstname = new JLabel("First_Name");
		lblFirstname.setBounds(213, 14, 288, 14);
		contentPane.add(lblFirstname);
		lblFirstname.setText(user.FirstName);
		
		lblLastname = new JLabel("Last_Name");
		lblLastname.setBounds(213, 39, 288, 14);
		contentPane.add(lblLastname);
		lblLastname.setText(user.LastName);
		
		lblEmail = new JLabel("Email");
		lblEmail.setBounds(213, 64, 288, 14);
		contentPane.add(lblEmail);
		lblEmail.setText(user.Email);
		
		txtProfilePictureUrl = new JTextField();
		txtProfilePictureUrl.setBounds(128, 219, 288, 20);
		contentPane.add(txtProfilePictureUrl);
		txtProfilePictureUrl.setColumns(10);
		
		txtProfilePictureUrl.setText(user.ProfilePicture);
		
		Image image = null;
		try {
		    URL url = new URL(user.ProfilePicture);
		    image = ImageIO.read(url);
		} catch (IOException e) {
		}
		
		final JLabel lblProfilePicture = new JLabel(new ImageIcon(image.getScaledInstance(-1, 150, java.awt.Image.SCALE_SMOOTH)));
		lblProfilePicture.setBounds(13, 14, 174, 194);
		contentPane.add(lblProfilePicture);
		
		JLabel lblProfilePictureUrl = new JLabel("Profile Picture Url:");
		lblProfilePictureUrl.setBounds(23, 222, 105, 14);
		contentPane.add(lblProfilePictureUrl);
		
		JButton btnSaveUrl = new JButton("Save");
		btnSaveUrl.setBounds(426, 218, 70, 23);
		contentPane.add(btnSaveUrl);
		
		JLabel lblWishList = new JLabel("Wish List");
		lblWishList.setBounds(599, 9, 64, 14);
		contentPane.add(lblWishList);
		
		JLabel lblPostsCommentedOn = new JLabel("Posts Commented On");
		lblPostsCommentedOn.setBounds(308, 239, 132, 14);
		contentPane.add(lblPostsCommentedOn);
		
		JLabel lblTopicsCreated = new JLabel("Topics Created");
		lblTopicsCreated.setBounds(69, 239, 100, 14);
		contentPane.add(lblTopicsCreated);
		
		//List of Comments
		final DefaultListModel commentsModel = new DefaultListModel();
		Topic[] comments = Profile.GetTopicsCommentedByUser(user.Id);
		for(int i = 0; i < comments.length; i++){
			commentsModel.addElement(new ListItem(comments[i].getItemId(), comments[i].getTopicName()));
		}
		final JList listComments = new JList(commentsModel);
		JScrollPane commentPane = new JScrollPane(listComments); 
		listComments.setBounds(268, 273, 212, 244);
		commentPane.setBounds(268, 273, 212, 244);
		contentPane.add(commentPane);
		MouseListener commentsMouseListener = new MouseAdapter() {
		     public void mouseClicked(MouseEvent e) {
		    	 if(e.getClickCount() == 2)
		    	 {
		    		 Topic topic = ViewTopic.GetTopic(((ListItem)listComments.getSelectedValue()).id);
		    		 view_topic x = new view_topic(topic);
		    		 x.setVisible(true);
		    		 dispose();
		    	 }
	          }
		 };
		 listComments.addMouseListener(commentsMouseListener);
		
		
		//List of Topics
		final DefaultListModel topicsModel = new DefaultListModel();
		Topic[] topics = Profile.GetTopicsCreatedByUser(user.Id);
		for(int i = 0; i < topics.length; i++){
			topicsModel.addElement(new ListItem(topics[i].getItemId(), topics[i].getTopicName()));
		}
		final JList listTopics = new JList(topicsModel);
		JScrollPane topicPane = new JScrollPane(listTopics); 
		topicPane.setBounds(23, 273, 212, 244);
		contentPane.add(topicPane);
		MouseListener topicsMouseListener = new MouseAdapter() {
		     public void mouseClicked(MouseEvent e) {
		    	 if(e.getClickCount() == 2)
		    	 {
		    		 Topic topic = ViewTopic.GetTopic(((ListItem)listTopics.getSelectedValue()).id);
		    		 view_topic x = new view_topic(topic);
		    		 x.setVisible(true);
		    		 dispose();
		    	 }
	          }
		 };
		 listTopics.addMouseListener(topicsMouseListener);
		
		
		//Wish List 
		final DefaultListModel wishListModel = new DefaultListModel();
		List<HashMap<String, Object>> results = Profile.GetWishlistByUserId(user.Id);
		for(int i = 0; i < results.size(); i++){
			wishListModel.addElement(new ListItem((int)results.get(i).get("item_id"), results.get(i).get("content").toString()));
		}
		final JList listWishList = new JList(wishListModel);
		JScrollPane wishPane = new JScrollPane(listWishList);
		wishPane.setBounds(522, 34, 237, 482);
		contentPane.add(wishPane);
		MouseListener wishListMouseListener = new MouseAdapter() {
		     public void mouseClicked(MouseEvent e) {
		    	 WishListPopup x = new WishListPopup(((ListItem)listWishList.getSelectedValue()).id, user.Id, listWishList.getSelectedValue().toString(), listWishList.getSelectedIndex(), wishListModel, isUser);
				 x.setVisible(true);
	          }
		 };
		 listWishList.addMouseListener(wishListMouseListener);
		
		btnSaveUrl.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						Profile.SaveProfileUrl(txtProfilePictureUrl.getText(), user.Id);
						Image image = null;
						try {
						    URL url = new URL(txtProfilePictureUrl.getText());
						    image = ImageIO.read(url);
						} catch (IOException ex) {
						}
						contentPane.remove(lblProfilePicture);
						JLabel lblProfilePicture = new JLabel(new ImageIcon(image.getScaledInstance(-1, 150, java.awt.Image.SCALE_SMOOTH)));
						lblProfilePicture.setBounds(13, 14, 174, 194);
						contentPane.add(lblProfilePicture);
						user.ProfilePicture = txtProfilePictureUrl.getText();
					}

				}
			);
		
		
		if(!isUser)
		{
			btnSaveUrl.setVisible(false);
			txtProfilePictureUrl.setVisible(false);
			lblProfilePictureUrl.setVisible(false);
		}
		
		if(Login.LoggedInUser.isAdmin){
			JButton btnBanUser = new JButton("Ban User");
			btnBanUser.setBounds(211, 185, 89, 23);
			contentPane.add(btnBanUser);
			btnBanUser.addActionListener(
					new ActionListener(){
						public void actionPerformed(ActionEvent e)
						{
							String[] choices = {"Day" , "Week", "Month", "Year", "Forever", "Remove Ban"};
							java.util.Date today = new java.util.Date();
							java.sql.Date sqlToday = new java.sql.Date(today.getTime());
					    	int response = JOptionPane.showOptionDialog(null
					    			, "How long would you like to ban this user?"
					                , "Ban User"
					                , JOptionPane.YES_NO_OPTION
					                , JOptionPane.PLAIN_MESSAGE
					                , null
					                , choices
					                , "None of your business");
					    	switch (response) {
					        case 0: 
					        	Profile.BanUser(new java.sql.Date(today.getTime() + 1 * 24 * 60 * 60 * 1000),user.Id);
					            break;
					        case 1:
					        	Profile.BanUser(new java.sql.Date(today.getTime() + 7 * 24 * 60 * 60 * 1000),user.Id);
					            break;
					        case 2: 
					        	Profile.BanUser(new java.sql.Date(today.getTime() + 30 * 24 * 60 * 60 * 1000),user.Id);
					            break;   
					        case 3: 
					        	Profile.BanUser(new java.sql.Date(today.getTime() + 365 * 24 * 60 * 60 * 1000),user.Id);
					            break;
					        case 4: 
					        	Profile.BanUser(new java.sql.Date(today.getTime() + 10000000 * 24 * 60 * 60 * 1000),user.Id);
					            break;
					        case 5: 
					        	Profile.RemoveBan(user.Id);
					            break;
					        case -1:
					            //... Both the quit button (3) and the close box(-1) handled here.
					            System.exit(0);     // It would be better to exit loop, but...
					        default:
					            //... If we get here, something is wrong.  Defensive programming.
					            JOptionPane.showMessageDialog(null, "Unexpected response " + response);
					    	}
						}

					}
				);
	}
	}
	public class ListItem  
	{  
	  int id;  
	  String description;  
	  public ListItem(int rn, String desc)  
	  {  
	    id = rn;  
	    description = desc;  
	  }  
	  public String toString(){return description;}  
	} 
}
